package com.ljtg.ljtgservice.config;
import com.ljtg.ljtgservice.interceptor.JwtTokenAdminInterceptor;
import com.ljtg.ljtgservice.interceptor.JwtTokenUserInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


import java.util.List;

/**
 * 配置类注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    @Autowired
    private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;

    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;

    /**
     * 注册自定义拦截器
      * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry){
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenAdminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");

        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/uszr/**")//测试需要故意打错user
                .excludePathPatterns("/user/uszr/login")
                .excludePathPatterns("/uszr/shop/status");
    }



    /**
     * 设置静态资源映射
     * @param registry
     */
     protected void addResourceHandlers(ResourceHandlerRegistry registry)   {
        log.info("开始设置静态资源映射...");
         registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
         registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
     }

    /**
     * 拓展Spring mvc 框架的小夕转化器
     * @param converters
     */
//    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//        log.info("拓展消息转换器");
//        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//
//        converter.setObjectMapper(new JacksonObjectMapper());
//
//        converters.add(0, converter);
//
}
